﻿using System;
using System.Collections.Generic;
using System.IO;

namespace Com.Lancetinc.Evs.Helpers
{
    public class I18nHelper
    {
        private static I18nHelper _default;
        public static I18nHelper Default
        {
            get
            {
                if (_default == null)
                {
                    _default = new I18nHelper();
                }
                return _default;
            }
        }

        public string Lang { get; private set; }

        public bool IsChinese => Lang == "cn";
        public bool IsEnglish => Lang == "en";

        public static string LabelStr = Default.IsChinese ? "：" : ": ";

        private Dictionary<string, string> mMessages;

        private I18nHelper()
        {
            mMessages = new Dictionary<string, string>();

            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "messages.ini");
            InitMessages(path);

            var lang = Properties.Settings.Default.Lang;
            if (!string.IsNullOrEmpty(lang))
            {
                Lang = lang;
                path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "messages_" + lang + ".ini");
                InitMessages(path);
            }
        }

        private void InitMessages(string path)
        {
            if (!File.Exists(path))
                return;

            var dic = ReadResourceFile(path);
            foreach (var kvp in dic)
            {
                mMessages[kvp.Key] = kvp.Value;
            }
        }

        /// <summary>
        /// 读取资源文件
        /// 忽略以#开头的文件
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static Dictionary<string, string> ReadResourceFile(string path)
        {
            var dic = new Dictionary<string, string>();

            var lines = File.ReadAllLines(path);
            foreach (var line in lines)
            {
                var s = line.TrimStart();
                if (s.Length <= 2 || s.StartsWith("#"))
                    continue;

                var eqIndex = s.IndexOf('=');
                if (eqIndex <= 1)
                    continue;

                var key = s.Substring(0, eqIndex).TrimEnd();
                if (key.Length > 0)
                {
                    var msg = s.Substring(eqIndex + 1).TrimStart();
                    msg = msg.Replace("\\n", "\n");
                    dic[key] = msg;
                }
            }

            return dic;
        }

        public bool HasMessage(string key)
        {
            return mMessages.ContainsKey(key);
        }

        public string GetMessage(string key)
        {
            var rawKey = key;
            while (!mMessages.ContainsKey(key))
            {
                int dotIndex = key.IndexOf('.');
                if (dotIndex < 0)
                {
                    break;
                }
                key = key.Substring(dotIndex + 1);
            }

            if (!mMessages.ContainsKey(key))
            {

#if DEBUG
                return $"#{rawKey}#";
#endif
                throw new Exception("cannot found message for '" + rawKey + "'");
            }
            return mMessages[key];
        }

        public string GetMessageWithArgs(string key, params object[] args)
        {
            var s = GetMessage(key);
            return string.Format(s, args);
        }
    }
}
